import axios from 'axios'

// 请求拦截-加上token
axios.interceptors.request.use((config) => {
  const token = localStorage.getItem('token')
  if (token) {
    // 如果 token 存在，在每个 HTTP header 都加上 token
    // Bearer 是 JWT 的认证头部信息
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
})

// 登录
const login = async (username, password, type) => {
  const response = await axios.post('/api/login', {
    username,
    password,
    type
  })
  return response
}
// 添加新区点
const setPoint = async (username, password, type, interestPoint) => {
  const response = await axios.post('/api/interstpoint', {
    username,
    password,
    type,
    interestPoint
  })
  return response
}
// 重置感兴趣点
const resetPoint = async (username, password, type, interestPoints) => {
  const response = await axios.post('/api/resetinterstpoint', {
    username,
    password,
    type,
    interestPoints
  })
  return response
}
// 获取用户数据
const getUser = (type) => {
  return axios({
    url: `/api/user?type=${type}`
  })
}

// 注册
const signin = async (username, password, type) => {
  const response = await axios.post('/api/register', {
    username,
    password,
    type
  })
  return response
}
// 修改密码
const Retrieve = async (username, password, type) => {
  const response = await axios.post('/api/Retrieve', {
    username,
    password,
    type
  })
  return response
}
// 登出
// get 和 post请求的封装还不一样，post似乎需要一个接受的变量，不能直接return
const logout = async (type, username) => {
  const response = await axios.post('/api/logout', {
    type,
    username
  })
  return response
}
// const logout = () => {
//   axios.post('/api/logout',{
//     type: 'common',
//     username: '赵六'
//   })
//   .then(function(res){
//     console.log(res,'res');
//   })
//   .catch(function(err){
//     console.log(err);
//   });
// }

// 获取公告
const getAnnouncement = () => {
  const type = 'Announcement'
  return axios({
    url: `/api/Announcement?type=${type}`
  })
}
// 添加公告
const addAnnouncement = async (username, password, type, title, content, Announcement) => {
  const response = await axios.post('/api/addAnnouncement', {
    username,
    password,
    type,
    title,
    content,
    Announcement
  })
  return response
}
// 删除公告
const removeAnnouncement = async (id) => {
  const response = await axios.post('/api/removeAnnouncement', {
    id
  })
  return response
}
// 获取路况
const getroadCondition = () => {
  return axios({
    url: `/api/roadCondition`
  })
}
// 报告路况
const addroadCondition = async (username, password, type, road, content, coordinate) => {
  const response = await axios.post('/api/addroadCondition', {
    username,
    password,
    type,
    road,
    content,
    coordinate
  })
  return response
}
// 删除路况
const removeroadCondition = async (id) => {
  const response = await axios.post('/api/removeroadCondition', {
    id
  })
  return response
}
export {
  login,
  getUser,
  logout,
  getAnnouncement,
  signin,
  setPoint,
  resetPoint,
  addAnnouncement,
  Retrieve,
  removeAnnouncement,
  getroadCondition,
  addroadCondition,
  removeroadCondition

}
